<?php

namespace App\Http\Controllers\Api;

use App\Common\Libs\ArrayHelper;
use App\Exceptions\Error;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;

class ReplyController extends BaseController
{
    public $validate = [
        "add" => ["rules" => [
            "parent_id" => "required|string|max:24",
            "content" => "required|string|max:1000",
        ]],
        "del" => ["rules" => [
            "id" => "required|string|max:24",
        ]],
        "getList" => ["rules" => [
            "comment_id" => "required|string|max:24",
            "page" => "sometimes|required",
        ]],
    ];

    /**
     * @path /reply/list
     * @category 回复
     * @title 列表
     * @method POST
     * @params [{"name":"comment_id","type":"text","required":1,"example":"","desc":"评论ID"},{"name":"page","type":"text","required":0,"example":"","desc":"页数，默认是0"}]
     * @resBody {"success":true,"statusCode":200,"result":{"total_count":1,"page_size":10,"page":1,"list":[{"id":"24位回复ID","content":"回复内容","is_deleted":0,"is_like":false,"created_at":1599198791,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像","attention_status":"关注状态，1没关注，2关注了，3自己，4互关"},"to_user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像","attention_status":"关注状态，1没关注，2关注了，3自己，4互关"},"count":{"reply":0,"like":0}}]},"message":"OK"}
     */
    public function getList(Request $request)
    {
        $params = $request->all();
        $searchParams = [
            'current_user_id' => $this->getCurrentUserId(),
            'comment_id' => $params['comment_id'],
            'page' => $params['page'] ?? 1,
        ];

        $replyList = service()->ReplyService->getReplyListByCommentId($searchParams);

        return $this->apiReturn($replyList);
    }

    /**
     * @path /reply/add
     * @category 回复
     * @title 新增
     * @method POST
     * @params [{"name":"parent_id","type":"text","required":1,"example":"","desc":"回复的是评论就是评论ID，回复的是回复就是回复ID"},{"name":"content","type":"text","required":1,"example":"","desc":"回复内容"}]
     * @resBody {"success":true,"statusCode":200,"result":{"id":"24位回复ID","content":"回复内容","is_deleted":0,"is_like":false,"created_at":1599198791,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像","attention_status":"关注状态，1没关注，2关注了，3自己，4互关"},"to_user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像","attention_status":"关注状态，1没关注，2关注了，3自己，4互关"},"count":{"reply":0,"like":0}},"message":"OK"}
     */
    public function add(Request $request)
    {
        $params = $request->all();
        $loadData = [
            'parent_id' => $params['parent_id'],
            'content' => $params['content'],
            'user_id' => $this->getCurrentUserId(),
        ];

        $replyInfo = service()->ReplyService->createReply($loadData);
        if (!$replyInfo) {
            return $this->apiReturn(Error::handle(lang('Target is disable')));
        }

        $result = ArrayHelper::merge($replyInfo, [
            'is_like' => 0,
            'count' => ['reply' => 0, 'like' => 0],
        ]);

        return $this->apiReturn($result);
    }

    /**
     * @path /reply/del
     * @category 回复
     * @title 删除
     * @method POST
     * @params [{"name":"id","type":"text","required":1,"example":"","desc":"回复ID"}]
     * @resBody {"success":true,"statusCode":200,"result":true,"message":"OK"}
     */
    public function del(Request $request)
    {
        $params = $request->all();

        $reply = service()->ReplyService->deleteReply($params['id'], $this->getCurrentUserId());

        return $this->apiReturn($reply);
    }
}
